package main

import (
	"strconv"
	"strings"

	"gitee.com/liuqiuming/gps"
	"github.com/beevik/etree"
	"github.com/tealeg/xlsx"
)

//小区信息工作协程
func infoWorker(row *xlsx.Row, ltefd *etree.Element) {
	lock.Lock()

	cellName := row.Cells[cellNameColNo].Value
	lon, _ := strconv.ParseFloat(row.Cells[lonColNo].Value, 64)
	lat, _ := strconv.ParseFloat(row.Cells[latColNo].Value, 64)
	azimuth := row.Cells[azColNo].Value
	freq := row.Cells[freqColNo].Value
	sector := strings.Split(azimuth, "/")
	outIn := row.Cells[outInColNo].Value
	var r float64

	switch outIn {
	case "室内":
		fallthrough
	case "Indoor":
		fallthrough
	case "室分小区":
		fallthrough
	case "lamp小区":
		fallthrough
	case "室分":
		r = 0
	default:
		r = getRadius(row)

	}

	//有几个功分扇区填几个扇区信息
	for i := 0; i < len(sector); i++ {
		placemark := ltefd.CreateElement("Placemark")
		name := placemark.CreateElement("name")
		name.CreateText(cellName)
		style := placemark.CreateElement("styleUrl")
		switch freq {
		case "1":
			style.CreateText("#msn_grn-blank0") //red
		case "3":
			style.CreateText("#msn_grn-blank") //white
		case "5":
			style.CreateText("#msn_grn-blank1") //green
		default:
			style.CreateText("#msn_grn-blank") //white
		}
		region := placemark.CreateElement("Region")
		box := region.CreateElement("LatLonAltBox")
		lonEastNorth, latEastNorth := gps.GetPoint(lon, lat, 45, 8000)
		lonWestSouth, latWestSouth := gps.GetPoint(lon, lat, 225, 8000)
		strLonEastNorth := strconv.FormatFloat(lonEastNorth, 'f', -1, 64)
		strLonEastNorth = substr(strLonEastNorth, 0, 9)
		strLatEastNorth := strconv.FormatFloat(latEastNorth, 'f', -1, 64)
		strLatEastNorth = substr(strLatEastNorth, 0, 9)
		strLonWestSouth := strconv.FormatFloat(lonWestSouth, 'f', -1, 64)
		strLonWestSouth = substr(strLonWestSouth, 0, 9)
		strLatWestSouth := strconv.FormatFloat(latWestSouth, 'f', -1, 64)
		strLatWestSouth = substr(strLatWestSouth, 0, 9)

		north := box.CreateElement("north")
		north.CreateText(strLatEastNorth)
		south := box.CreateElement("south")
		south.CreateText(strLatWestSouth)
		east := box.CreateElement("east")
		east.CreateText(strLonEastNorth)
		west := box.CreateElement("west")
		west.CreateText(strLonWestSouth)
		min := box.CreateElement("minAltitude")
		min.CreateText("0")
		max := box.CreateElement("maxAltitude")
		max.CreateText("0")
		lod := region.CreateElement("Lod")
		minLodPixels := lod.CreateElement("minLodPixels")
		minLodPixels.CreateText("1028")
		maxLodPixels := lod.CreateElement("maxLodPixels")
		maxLodPixels.CreateText("-1")
		minFadeExtent := lod.CreateElement("minFadeExtent")
		minFadeExtent.CreateText("0")
		maxFadeExtent := lod.CreateElement("maxFadeExtent")
		maxFadeExtent.CreateText("0")
		point := placemark.CreateElement("Point")
		coord := point.CreateElement("coordinates")
		az, _ := strconv.ParseFloat(sector[i], 64)
		lon1, lat1 := gps.GetPoint(lon, lat, az, r/2) //通过经纬度、方位角、距离计算目标点经纬度
		strLon := strconv.FormatFloat(lon1, 'f', -1, 64)
		strLon = substr(strLon, 0, 9)
		strLat := strconv.FormatFloat(lat1, 'f', -1, 64)
		strLat = substr(strLat, 0, 8)
		coord.CreateText(strLon + "," + strLat + "," + "8")
	}

	lock.Unlock()
	wg.Done()
}

//小区图形工作协程
func sectorWorker(head *xlsx.Row, row *xlsx.Row, maxCol int, fd *etree.Element) {
	lock.Lock()

	cellName := row.Cells[cellNameColNo].Value
	lon, _ := strconv.ParseFloat(row.Cells[lonColNo].Value, 64)
	lat, _ := strconv.ParseFloat(row.Cells[latColNo].Value, 64)
	azimuth := row.Cells[azColNo].Value
	freq := row.Cells[freqColNo].Value
	outIn := row.Cells[outInColNo].Value

	beam := 30.00
	switch freq {
	case "1":
		beam = 26
	case "3":
		beam = 20
	case "5":
		beam = 10
	case "3.5G": //5G新增
		beam = 20
	case "2.1G":
		beam = 14 //5G新增
	default:
		beam = 20
	}
	//有功分的方位角以“/”分隔
	sector := strings.Split(azimuth, "/")
	r := getRadius(row)

	//有几个功分画几个扇区图形
	for i := 0; i < len(sector); i++ {
		placemark := fd.CreateElement("Placemark")
		name := placemark.CreateElement("name")
		name.CreateText(cellName)
		vis := placemark.CreateElement("visibility")
		vis.CreateText("1")
		disc := placemark.CreateElement("description")
		//增加表格数据，每4列作为一行
		table := "<table border=1 width=650>" + "<caption><h2>" + cellName + "</h2></caption>"
		column := 0
		group := maxCol / 4
		//每4个为一组
		for j := 0; j < group; j++ {
			//一行4个标题
			table = table + "<tr>"
			for k := 0; k < 4; k++ {
				//重要字段加红处理
				if isNeeded(column + k) {
					table = table + "<th>" + "<font color=#ff0000>" + head.Cells[column+k].Value + "(*)" + "</font>" + "</th>"
				} else {
					table = table + "<th>" + head.Cells[column+k].Value + "</th>"
				}
			}
			table = table + "</tr>"
			//一行4个内容
			table = table + "<tr>"
			for k := 0; k < 4; k++ {
				//日期数据，特殊处理
				if (column+k) == updateDateColNo || (column+k) == testDateColNo {
					strDate := ""
					if row.Cells[column+k].Value != "" {
						strDate = convertToFormatDay(row.Cells[column+k].Value)
					}

					table = table + "<td>" + strDate + "</td>"
				} else {
					if (column+k) == lonColNo || (column+k) == latColNo {
						str := substr(row.Cells[column+k].Value, 0, 10)
						table = table + "<td>" + str + "</td>"
					} else {
						table = table + "<td>" + row.Cells[column+k].Value + "</td>"
					}

				}

			}
			table = table + "</tr>"

			column = column + 4
		}
		//余下至多三个列标题
		colMod := maxCol % 4
		table = table + "<tr>"
		for y := 0; y < colMod; y++ {
			table = table + "<th>" + head.Cells[group*4+y].Value + "</th>"
		}
		//增加空列
		for m := 0; m < 4-colMod; m++ {
			table = table + "<th></th>"
		}
		table = table + "</tr>"

		//余下至多三个列内容
		table = table + "<tr>"
		for x := 0; x < colMod; x++ {
			//如果是日期数据，特殊处理
			if (group*4+x) == updateDateColNo || (group*4+x) == testDateColNo {
				strDate := ""
				if row.Cells[group*4+x].Value != "" {
					strDate = convertToFormatDay(row.Cells[group*4+x].Value)
				}

				table = table + "<td>" + strDate + "</td>"
			} else {

				table = table + "<td>" + row.Cells[group*4+x].Value + "</td>"
			}
			//	fmt.Printf("第%d列", group*4+x)
		}

		//增加空列
		for m := 0; m < 4-colMod; m++ {
			table = table + "<td></td>"
		}
		table = table + "</tr>"

		table = table + "</table>"
		disc.CreateCData(table)
		style := placemark.CreateElement("styleUrl")
		//PCI模3，不同颜色
		pci, _ := strconv.ParseInt(row.Cells[pciColNo].Value, 10, 64)
		mod3 := pci % 3
		switch mod3 {
		case 0:
			style.CreateText("#msn_ylw-pushpin1") //red
		case 1:
			style.CreateText("#msn_ylw-pushpin0") //green
		case 2:
			style.CreateText("#msn_ylw-pushpin2") //blue
		default:
			style.CreateText("#msn_ylw-pushpin") //black
		}
		/*
			region := placemark.CreateElement("Region")
			box    := region.CreateElement("LatLonAltBox")
			lonEastNorth,latEastNorth := gps.GetPoint(lon,lat,45,5000)
			lonWestSouth,latWestSouth := gps.GetPoint(lon,lat,225,5000)
			strLonEastNorth := strconv.FormatFloat(lonEastNorth, 'f', -1, 64)
			strLonEastNorth = substr(strLonEastNorth, 0, 9)
			strLatEastNorth := strconv.FormatFloat(latEastNorth, 'f', -1, 64)
			strLatEastNorth = substr(strLatEastNorth, 0, 9)
			strLonWestSouth := strconv.FormatFloat(lonWestSouth, 'f', -1, 64)
			strLonWestSouth = substr(strLonWestSouth, 0, 9)
			strLatWestSouth := strconv.FormatFloat(latWestSouth, 'f', -1, 64)
			strLatWestSouth = substr(strLatWestSouth, 0, 9)

			north  := box.CreateElement("north")
			north.CreateText(strLatEastNorth)
			south  := box.CreateElement("south")
			south.CreateText(strLatWestSouth)
			east   :=box.CreateElement("east")
			east.CreateText(strLonEastNorth)
			west   :=box.CreateElement("west")
			west.CreateText(strLonWestSouth)
			min    := box.CreateElement("minAltitude")
			min.CreateText("0")
			max    := box.CreateElement("maxAltitude")
			max.CreateText("0")
			lod    := region.CreateElement("Lod")
			minLodPixels := lod.CreateElement("minLodPixels")
			minLodPixels.CreateText("1028")
			maxLodPixels := lod.CreateElement("maxLodPixels")
			maxLodPixels.CreateText("-1")
			minFadeExtent := lod.CreateElement("minFadeExtent")
			minFadeExtent.CreateText("0")
			maxFadeExtent := lod.CreateElement("maxFadeExtent")
			maxFadeExtent.CreateText("0")
		*/

		polygon := placemark.CreateElement("Polygon")
		tes := polygon.CreateElement("tessellate")
		tes.CreateText("1")
		outb := polygon.CreateElement("outerBoundaryIs")
		line := outb.CreateElement("LinearRing")
		coord := line.CreateElement("coordinates")
		coordinates := ""
		switch outIn {
		case "室外宏站":
			fallthrough
		case "宏小区":
			fallthrough
		case "微站":
			fallthrough
		case "室外":
			fallthrough
		case "Outdoor":
			fallthrough
		case "室外小站": //室外扇形，取【原点，扇形边点，中点，扇形边点，原点】共5个点组成扇形
			az, _ := strconv.ParseFloat(sector[i], 64)
			lon2, lat2 := gps.GetPoint(lon, lat, az-beam, r) //扇形边点
			lon3, lat3 := gps.GetPoint(lon, lat, az, r)      //扇形中点
			lon4, lat4 := gps.GetPoint(lon, lat, az+beam, r) //扇形边点
			strLon := row.Cells[lonColNo].Value              //原点经度，直接EXCEL表格数据
			strLon = substr(strLon, 0, 9)
			strLat := row.Cells[latColNo].Value //原点纬度，直接EXCEL表格数据
			strLat = substr(strLat, 0, 8)
			strLon2 := strconv.FormatFloat(lon2, 'f', -1, 64)
			strLon2 = substr(strLon2, 0, 9)
			strLat2 := strconv.FormatFloat(lat2, 'f', -1, 64)
			strLat2 = substr(strLat2, 0, 8)
			strLon3 := strconv.FormatFloat(lon3, 'f', -1, 64)
			strLon3 = substr(strLon3, 0, 9)
			strLat3 := strconv.FormatFloat(lat3, 'f', -1, 64)
			strLat3 = substr(strLat3, 0, 8)
			strLon4 := strconv.FormatFloat(lon4, 'f', -1, 64)
			strLon4 = substr(strLon4, 0, 9)
			strLat4 := strconv.FormatFloat(lat4, 'f', -1, 64)
			strLat4 = substr(strLat4, 0, 8)
			coordinates = strLon + "," + strLat + ",0 " + strLon2 + "," + strLat2 + ",0 " + strLon3 + "," + strLat3 + ",0 " + strLon4 + "," + strLat4 + ",0 " + strLon + "," + strLat + ",0"
			coord.CreateText(coordinates)

		case "室内": //室分画一个12点的圆
			fallthrough
		case "Indoor":
			fallthrough
		case "lamp小区":
			fallthrough
		case "室分小区":
			fallthrough
		case "室分":
			coordinates = ""
			for i := 0; i < 12; i++ {
				lon2, lat2 := gps.GetPoint(lon, lat, float64(i*30), 30)
				strLon2 := strconv.FormatFloat(lon2, 'f', -1, 64)
				strLon2 = substr(strLon2, 0, 9)
				strLat2 := strconv.FormatFloat(lat2, 'f', -1, 64)
				strLat2 = substr(strLat2, 0, 8)
				coordinates = coordinates + strLon2 + "," + strLat2 + ",0 "
			}
			coord.CreateText(coordinates)

		}

	}

	lock.Unlock()
	wg.Done()
}
